package hjt.com.module_test.widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.LinearInterpolator;

import com.tong.lib.utils.SizeUtils;

import androidx.annotation.Nullable;

public class WaveView extends BaseView {

    private Paint paint;
    private int vWidth;
    private int vHeight;
    private int waveWidth;
    private int waveHeight;
    private Path path = new Path();
    private ValueAnimator valueAnimator;
    private float animatedValue;
    private String TAG = "waveView";
    private Path circlePath = new Path();

    public WaveView(Context context) {
        this(context, null);
    }

    public WaveView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WaveView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setStyle(Paint.Style.FILL);
        paint.setAntiAlias(true);
        paint.setColor(Color.RED);
        paint.setTextSize(SizeUtils.sp2px(150));


        valueAnimator = new ValueAnimator();
        valueAnimator.setFloatValues(0, waveWidth);
        valueAnimator.setDuration(1000);
        valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
        valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                animatedValue = (float) animation.getAnimatedValue();
                invalidate();
            }
        });
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Log.e(TAG, "onMeasure");
        setMeasuredDimension(getSize(widthMeasureSpec, getResources().getDisplayMetrics().widthPixels), getSize(heightMeasureSpec, getResources()
                .getDisplayMetrics().heightPixels));
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        Log.e(TAG, "onSizeChanged");
        vWidth = w;
        vHeight = h;
        waveWidth = vWidth / 2;
        waveHeight = (int) (0.08 * vHeight);
        valueAnimator.setFloatValues(0, waveWidth);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        Log.e(TAG, "onAttachedToWindow");
        strart();
    }

    private void strart() {
        if (!valueAnimator.isRunning() && valueAnimator != null) {
            valueAnimator.start();
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        Log.e(TAG, "onDetachedFromWindow");
        if (valueAnimator.isRunning()) {
            valueAnimator.cancel();
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setColor(Color.RED);
        drawText(canvas,paint,"龙");

        path.reset();
        path.moveTo(-waveWidth + animatedValue, vHeight / 2);
        for (int i = 0; i < 3; i++) {
            path.rQuadTo(waveWidth / 4, waveHeight, waveWidth / 2, 0);
            path.rQuadTo(waveWidth / 4, -waveHeight, waveWidth / 2, 0);
        }

        path.lineTo(vWidth, vHeight);
        path.lineTo(0, vHeight);
        path.close();

        circlePath.reset();
        circlePath.addCircle(waveWidth,waveWidth,waveWidth, Path.Direction.CCW);
        circlePath.op(path, Path.Op.INTERSECT);
        canvas.drawPath(circlePath, paint);
        canvas.clipPath(circlePath);

        paint.setColor(Color.GREEN);
        drawText(canvas,paint,"龙");

    }

    private void drawText(Canvas canvas, Paint textPaint, String text) {
        RectF rect = new RectF(0, 0, vWidth, vHeight);
        textPaint.setTextAlign(Paint.Align.CENTER);
        Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
        float top = fontMetrics.top;
        float bottom = fontMetrics.bottom;
        float centerY = rect.centerY() - top / 2f - bottom / 2f;
        canvas.drawText(text, rect.centerX(), centerY, textPaint);
    }
}
